﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using A2DFramework.CommandBusService.MSMQCommandBus.Configuration;
using System.IO;
using System.Threading;

namespace A2DFramework.CommandBusService.MSMQCommandBus
{
    public class NetworkShareFileSaver
    {
        public static string Save(string xml)
        {
            string fileName=MSMQCommandBusConfiguration.NetworkLocation+"\\"+Guid.NewGuid().ToString("N");

            using (StreamWriter sw = new StreamWriter(fileName))
            {
                sw.Write(xml);
                sw.Flush();
            }

            return fileName;
        }

        public static string GetContent(string path)
        {
            string content = "";
            using (StreamReader sr = new StreamReader(path))
            {
                content = sr.ReadToEnd();
            }
            if (MSMQCommandBusConfiguration.AutoDeleteNetworkFile)
                ThreadPool.QueueUserWorkItem(new WaitCallback(DeleteFile), path);
            
            return content;
        }
        private static void DeleteFile(object state)
        {
            string path = (string)state;
            File.Delete(path);
        }
    }
}
